<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="400px"
    append-to-body
    :before-close="close"
  >
    <div class="qrcode-container">
      <div class="qrcode-container__qrcode" v-loading="loading">
        <QrCode :url="form.url" ref="qrcode"/>
      </div>
      <div class="handle-button">
        <el-button type="text" size="mini" @click="flush" icon="el-icon-refresh-right">刷新</el-button>
      </div>
    </div>

  </el-dialog>
</template>

<script>
import { debounce } from '@/utils'
import QrCode from '@/components/QrCode'
import { signingProprietorContract } from '@/api/contract/proprietor'

export default {
  name: 'view-qrcode',
  components: {
    QrCode
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    title: {
      default: '签署二维码'
    },
    data: {}
  },
  data() {
    return {
      loading: false,
      form: {
        url: ''
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      this.loading = true
      console.log('重置')
      let params = {
        proprietorContractId: this.data.proprietorContractId,
        redirectUrl: ''
      }
      signingProprietorContract(params).then((response) => {
        this.form.url = response.data
        this.$nextTick(() => {
          this.$refs.qrcode.getQRCode()
          this.loading = false
        })
        console.log('重置1')
      })
      this.resetForm('form')
    },

    // 刷新二维码
    flush: debounce(function() {
      this.reset()
    }, 500),
    close() {
      this.reset()
      this.visible = false
    }
  }
}
</script>


<style scoped lang="scss">
::v-deep .el-dialog__body {
  max-height: 80vh !important;
  overflow-y: auto;
}

.qrcode-container {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  height: 100%;

  .qrcode-container__qrcode {
    height: 200px;
    height: 200px;
  }
}

</style>
